import {Injectable} from '@angular/core';
import {Http} from '@angular/http';

import {CustomerProvider} from "./customerProvider";
import {Cart} from "../models/Cart";
import {Observable} from "rxjs/Observable";
import {url_order} from "../config";
import {OrderNumber} from "../models/OrderNumber";

import {EmployeeProvider} from "./employeeProvider";
import 'rxjs/add/operator/map';

/*
 Generated class for   the OrderProvider provider.

 See https://angular.io/docs/ts/latest/guide/dependency-injection.html
 for more info on providers and Angular 2 DI.
 */
@Injectable()
export class OrderProvider {


  constructor(public http: Http,
              public customer: CustomerProvider,
              public employee: EmployeeProvider,) {
  }

  createOrder(carts: Cart[], tax = 0, tips = 0): Observable<any> {
    let order_info = {carts: carts, ext: {Tax: tax, Tips: tips}};
    return this.http.post(url_order, JSON.stringify(order_info)).map(res => {
      console.log(res.json());
      return res.json();
    });
  }

  getOrders(page = 1, store_id = null, table_id = null): Observable<any> {
    let link = url_order + "?page=" + page;
    if (this.employee.check()) {
      link = link + '&Waiter_Employee_ID=' + this.employee.employeeId();
      if (store_id != null)
        link = link + '&Store_ID=' + store_id;
      if (table_id != null)
        link = link + '&Table_ID=' + table_id;
    }
    else if (this.customer.check()) {
      link = link + "&Customer_ID=" + this.customer.userId();
    }
    console.log(link);
    return this.http.get(link).map(res => {
      console.log(res.json());
      return res.json();
    });
  }


  payment(order: OrderNumber): Observable<any> {
    let link = url_order + '/' + order.Order_ID + '/payment';
    return this.http.get(link).map(res => res.json());
  }

  cancel(order: OrderNumber): Observable<any> {
    let link = url_order + '/' + order.Order_ID + '/cancel';
    return this.http.get(link).map(res => res.json());
  }

  delete(order: OrderNumber): Observable<any> {
    let link = url_order + '/' + order.Order_ID + '/delete';
    return this.http.get(link).map(res => res.json());
  }

}
